package com.nep.NEPG.common;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {
    public static Map readMapObject(String filename){
        File file = new File(filename);
        Map map = null;
        try{
            if (!file.exists()||file.length() == 0){
                return new HashMap<>();
            }else {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                map = (Map) ois.readObject();
            }
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return map;
    }


    public static void writeMapObject(String fileName , Map map){
        try {
            File file = new File(fileName);
            if(!file.exists()){//如果文件不存在，则创建一个新文件
                file.createNewFile();
            }
            //构建一个文件输出流
            FileOutputStream fos = new FileOutputStream(file);
            //构建一个对象输出流
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            oos.flush();
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
}
